package com.gaof.hookactivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2018/6/19.
 */

public class HookUtil {
    private static final String TAG = "HookUtil";

    private Context context;

    public void hookAms(Context context){
        try {
            this.context=context.getApplicationContext();
            Class<?> ActivityManagerNativeClss = Class.forName("android.app.ActivityManagerNative");

            Field defaultFiled = ActivityManagerNativeClss.getDeclaredField("gDefault");
            defaultFiled.setAccessible(true);
            //静态变量，拿到的是默认值
            Object defaultValue = defaultFiled.get(null);
            //反射SingleTon
            Class<?> SingletonClass = Class.forName("android.util.Singleton");
            Field mInstance = SingletonClass.getDeclaredField("mInstance");
            mInstance.setAccessible(true);

            //到这里已经拿到ActivityManager对象
            Object iActivityManagerObject=mInstance.get(defaultValue);

            //开始动态代理，用代理对象替换掉真实的ActivityManager
            Class<?> IActivityManagerIntercept = Class.forName("android.app.IActivityManager");

            Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{IActivityManagerIntercept},
                    new StartActivityInvocationHandler(iActivityManagerObject));

            //现在替换掉这个对象
            mInstance.set(defaultValue, proxy);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    class StartActivityInvocationHandler implements InvocationHandler{

        private Object iActivityManagerObject;

        public StartActivityInvocationHandler(Object iActivityManagerObject) {
            this.iActivityManagerObject = iActivityManagerObject;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if("startActivity".equals(method.getName())){
                Log.e(TAG,"invoke");
                Intent intent=null;
                int index=0;
                for (int i=0;i<args.length;i++){
                    Object arg=args[i];
                    if(arg instanceof Intent){
                        intent= (Intent) arg;
                        index=i;
                        break;
                    }
                }
                if(intent!=null){
                    SharedPreferences share=context.getSharedPreferences("gaof",Context.MODE_PRIVATE);
                    String activityName=intent.getComponent().getClassName();
                    if(activityName.contains("MainActivity")||share.getBoolean("login",false)){//已登录
                        return method.invoke(iActivityManagerObject,args);
                    }else {//未登录
                        ComponentName componentName=new ComponentName(context,LoginActivity.class);
                        Intent loginIntent=new Intent();
                        loginIntent.setComponent(componentName);
                        loginIntent.putExtra("className",activityName);
                        args[index]=loginIntent;
                        return method.invoke(iActivityManagerObject,args);
                    }
                }
            }
            return method.invoke(iActivityManagerObject,args);
        }
    }

}
